var async = require('async')

module.exports = function(app) {
  return new Handler(app)
}

var Handler = function(app) {
  this.app = app
  Handler.app = app
  this.sessionService = this.app.get('sessionService')
  this.channelService = this.app.get('channelService')
}

var handler = Handler.prototype
var local = {}

//增加钻石
handler.addDiamond = function(msg,session,next) {
	local.check(session,function(flag) {
		if(!flag){
			next(null,{flag : false,err : "limits error"})
			return
		}
		var value = msg.value
		var uid = msg.uid
		Handler.app.rpc.db.remote.incrbyPlayer(null,uid,"diamond",value,function(flag) {
			next(null,{flag : flag})
		})
	})
}
//获取玩家信息
handler.getPlayerInfoByUid = function(msg,session,next) {
	local.check(session,function(flag) {
		if(!flag){
			next(null,{flag : false,err : "limits error"})
			return
		}
		var diamond = msg.diamond
		var uid = msg.uid
		Handler.app.rpc.db.remote.getPlayerInfoByUid(null,uid,function(data) {
			if(data){
				next(null,{flag : true,data : data})
			}else{
				next(null,{flag : false})
			}
		})
	})
}
//权限检查
local.check = function(session,cb) {
	var uid = session.get("uid")
	if(!uid){
		console.log("uid : "+uid)
		cb(false)
		return
	}
	//权限验证
	Handler.app.rpc.db.remote.getValue(null,uid,"limits",function(data) {
		console.log("limits : "+data)
		if(data >= 10){
			cb(true)
		}else{
			cb(false)
		}
	})
}